﻿using System;

namespace ZYCommon
{
    

    public class ConsoleBuffer
    {
        private int intBufferLength = 0;
        private int intDataLength = 0;
        private int intStartIndex = 0;
        private char[] myBuffer = new char[0x1388];

        public ConsoleBuffer()
        {
            this.intBufferLength = this.myBuffer.Length;
        }

        public void Clear()
        {
            lock (this)
            {
                this.intStartIndex = 0;
                this.intDataLength = 0;
                for (int i = 0; i < this.intBufferLength; i++)
                {
                    this.myBuffer[i] = ' ';
                }
            }
        }

        public override string ToString()
        {
            lock (this)
            {
                if ((this.intStartIndex + this.intDataLength) < this.intBufferLength)
                {
                    return new string(this.myBuffer, this.intStartIndex, this.intDataLength);
                }
                char[] chArray = new char[this.intDataLength];
                for (int i = 0; i < this.intDataLength; i++)
                {
                    chArray[i] = this.myBuffer[(i + this.intStartIndex) % this.intBufferLength];
                }
                return new string(chArray);
            }
        }

        public void Write(object obj)
        {
            this.Write(Convert.ToString(obj));
        }

        public void Write(string strText)
        {
            if ((strText != null) && (strText.Length > 0))
            {
                lock (this)
                {
                    int length = strText.Length;
                    int num2 = (length > this.intBufferLength) ? (length - this.intBufferLength) : 0;
                    int num3 = this.intStartIndex + this.intDataLength;
                    int num4 = num2;
                    while (num4 < length)
                    {
                        this.myBuffer[num3 % this.intBufferLength] = strText[num4];
                        num4++;
                        num3++;
                    }
                    this.intDataLength += length - num2;
                    if (this.intDataLength > this.intBufferLength)
                    {
                        this.intStartIndex = (this.intStartIndex + (this.intDataLength - this.intBufferLength)) % this.intBufferLength;
                        this.intDataLength = this.intBufferLength;
                    }
                }
            }
        }

        public void WriteLine(string strText)
        {
            this.Write("\r\n");
            this.Write(strText);
        }

        public int BufferLength
        {
            get
            {
                return this.intBufferLength;
            }
            set
            {
                if (value > 0)
                {
                    lock (this)
                    {
                        this.myBuffer = new char[value];
                        this.intBufferLength = value;
                        this.intStartIndex = 0;
                        this.intDataLength = 0;
                    }
                }
            }
        }
    }
}

